﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace VB2CS
{
    public sealed class Strings
    {
        public static int Len(string str)
        {
            //if (str == null||str==string.Empty) return 0;
            //return str.Length;
            return (str ?? String.Empty).Length;
        }
        public static int Len(object obj)
        {
            if (obj == null) return 0;
            else return Len(obj.ToString());
        }
        public static string[] Split(String source, String split, int limit, CompareMethod method)
        {
            string[] splitString={split};
            return source.Split(splitString,StringSplitOptions.RemoveEmptyEntries);
        }

        public static int InStr(int start, string searchString, string searchChar, CompareMethod compareMethod)
        {
            return searchString.IndexOf(searchChar, start);
        }

        public static int InStr( string searchString, string searchChar, CompareMethod compareMethod)
        {
            return searchString.IndexOf(searchChar);
        }


        //public static string FormatCurrency(decimal Expression, int NumDigitsAfterDecimal, TriState IncludeLeadingDigit, TriState UseParensForNegativeNumbers, TriState GroupDigits)
        //{
        //    throw new NotImplementedException();
        //}


        public static string FormatCurrency(decimal _expression, params object[] paramsArray)
        {
            int numDigitsAfterDecimal = -1;
            TriState includeLeadingDigit = TriState.UseDefault;
            TriState useParensForNegativeNumbers = TriState.UseDefault;
            TriState groupDigits = TriState.UseDefault;
            object[] temp = new object[] { numDigitsAfterDecimal, includeLeadingDigit, useParensForNegativeNumbers, groupDigits };
            ParamsArrayUtils.FillParamsArray(paramsArray, temp);
            numDigitsAfterDecimal = (int)temp[0];
            includeLeadingDigit = (TriState)temp[1];
            useParensForNegativeNumbers = (TriState)temp[2];
            groupDigits = (TriState)temp[3];

            return _expression.ToString("C");
        }


        public static string FormatDateTime(DateTime _dateTime, DateFormat _format)
        {
            //throw new NotImplementedException();
            //return String.Format("", _dateTime);
            //return _dateTime.ToString(_format.ToString());
            switch (_format)
            {
                case DateFormat.LongDate:
                    return _dateTime.ToLongDateString();
                case DateFormat.LongTime:
                    return _dateTime.ToLongTimeString();
                case DateFormat.ShortDate:
                    return _dateTime.ToShortDateString();
                case DateFormat.ShortTime:
                    return _dateTime.ToShortTimeString();
                case DateFormat.GeneralDate:
                default:
                    return _dateTime.ToString();
            }
        }

        //public static string Replace(string expression, string oldString, string newString, int start, int count, CompareMethod compareMethod)
        //{
        //    //throw new NotImplementedException();

        //}
        public static string Replace(string expression, string oldString, string newString, params  object[] paramsArray)
        {
            int start = -1;
            int count = -1;
            CompareMethod compareMethod = CompareMethod.Text;
            switch (paramsArray.Length)
            {
                case 1:
                    start = Convert.ToInt32(paramsArray[0]);
                    break;
                case 2:
                    start = Convert.ToInt32(paramsArray[0]);
                    count = Convert.ToInt32(paramsArray[1]);
                    break;
                case 3:
                    start = Convert.ToInt32(paramsArray[0]);
                    count = Convert.ToInt32(paramsArray[1]);
                    compareMethod = (CompareMethod)(Convert.ChangeType(paramsArray[2], typeof(CompareMethod)));
                    break;
                default:
                    throw new System.NotSupportedException("the params count should between 3 and 6");
            }
            return expression.Replace(oldString, newString);
        }

        public static string Right(string str, int length)
        {
            return str.Substring(0, length);
        }


        public static int Asc(string p)
        {
            //throw new NotImplementedException();
            return Convert.ToInt32(p[0]);
        }

        /// <summary>
        /// You should never use this function any more, try to use some clear format style.
        /// </summary>
        /// <param name="p">The object need to format.</param>
        /// <param name="p_2">The format string.</param>
        /// <returns></returns>
        public static string Format(object p, string p_2)
        {
            //throw new NotImplementedException();
            return string.Format(p_2, p);
        }

        public static int InStrRev(string sourceString, string checkString, int start, CompareMethod compareMethod)
        {
            //throw new NotImplementedException();
            return sourceString.LastIndexOf(checkString, start);
        }

        public static string Left(string URI, int p)
        {
            //throw new NotImplementedException();
            return URI.Substring(0, p);
        }

        public static string Mid(string sourceString, int start)
        {
            return sourceString.Substring(start);
        }

        public static string Mid(string sourceString, int start, int length)
        {
            return sourceString.Substring(start, length);        
        }
        

        public static string Trim(string p)
        {
            //throw new NotImplementedException();
            return p.Trim();
        }

        public static string LCase(string BodyType)
        {
            //throw new NotImplementedException();
            return BodyType.ToLower();
        }

        public static string Space(int number)
        {
            //throw new NotImplementedException();
            char[] space=new char[number];
            for(int i=0; i<number; ++i) space[i]=' ';
            Debug.Assert(space.ToString().Length != number, "need new string(char[] value)?");
            return space.ToString();
            //new string(space);            
        }

        public static string Join(string[] sourceArray, string separator)
        {
            return string.Join(separator, sourceArray);
        }
    }
}
